package com.lgh.demo.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.BitmapCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import org.json.JSONObject;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by oo on 2017/9/20.
 */

public class NetUtils {

    private static String TAG = "NetUtils";

    public static void getImage(Context context, final ImageView imageView, String path) {
        OkGo.<Bitmap>get(path).cacheMode(CacheMode.NO_CACHE).tag(context).execute(new BitmapCallback() {
            @Override
            public void onSuccess(Response<Bitmap> response) {
                imageView.setImageBitmap(response.body());
            }
        });
    }

    public static void getData(Context context, final String path, Map<String, String> params, final Class className, final NetCallBack callBack) {
        getData(context, path, 0, params, className, "加载中", callBack);
    }

    public static void getData(Context context, final String path, final int reqTag, Map<String, String> params, final Class className, final NetCallBack callBack) {
        getData(context, path, reqTag, params, className, "加载中", callBack);
    }

    public static void getData(final Context context, final String path, final int reqTag, Map<String, String> params, final Class className, String loadingStr, final NetCallBack callBack) {
        getData(context, path, reqTag, params, null, className, "加载中", callBack);
    }

    public static void getData(final Context context, final String path, final int reqTag, Map<String, String> params, String JSESSIONID,
                               final Class className, String loadingStr, final NetCallBack callBack) {

        GetRequest request = OkGo.<String>get(path).cacheMode(CacheMode.NO_CACHE).tag(context).params(params);
        if (JSESSIONID!=null){
            request.headers("JSESSIONID", JSESSIONID);
        }
        request.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                Log.w(TAG, "onSuccess: "+response.body());
                if (className==null){
                    if (callBack != null) {
                        callBack.netBack(reqTag, response.body());
                    }
                }else{
                    Object object = JSON.parseObject(response.body(), className);
                    if (object==null){
                        Toast.makeText(context, "网络连接失败", Toast.LENGTH_LONG).show();
                    }else if (callBack != null) {
                        callBack.netBack(reqTag, object);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    public static void postData(Context context, final String path, final int reqTag, Map<String, String> params, final Class className, final NetCallBack callBack) {
        postData(context, path, reqTag, params, className, "加载中...", callBack);
    }

    public static void postWithImgData(final Context context, final String path, final int reqTag, Map<String, String> params, File file, String fileName,
                                       final Class className, final NetCallBack callBack) {


        OkGo.<String>post(path).cacheMode(CacheMode.NO_CACHE).tag(context).params(params).params(fileName, file).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                Log.w(TAG, "onSuccess: "+response.body());
                if (className==null){
                    if (callBack != null) {
                        callBack.netBack(reqTag, response.body());
                    }
                }else{
                    Object object = JSON.parseObject(response.body(), className);
                    if (object==null){
                        Toast.makeText(context, "网络连接失败", Toast.LENGTH_LONG).show();
                    }else if (callBack != null) {
                        callBack.netBack(reqTag, object);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    public static void postWithImgListData(final Context context, final String path, final int reqTag, Map<String, String> params, List<String> pathList,
                                           final Class className, final NetCallBack callBack) {


        PostRequest<String> request =  OkGo.<String>post(path).cacheMode(CacheMode.NO_CACHE).tag(context).params(params);
        for (int i=0; i<pathList.size(); i++){
            request.params("img"+(i+1), new File(pathList.get(i)));
        }
        request.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                Log.w(TAG, "onSuccess: "+response.body());
                if (className==null){
                    if (callBack != null) {
                        callBack.netBack(reqTag, response.body());
                    }
                }else{
                    Object object = JSON.parseObject(response.body(), className);
                    if (object==null){
                        Toast.makeText(context, "网络连接失败", Toast.LENGTH_LONG).show();
                    }else if (callBack != null) {
                        callBack.netBack(reqTag, object);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    public static void postData(final Context context, final String path, final int reqTag, Map<String, String> params, final Class className, String loadingStr, final NetCallBack callBack) {

        OkGo.<String>post(path).cacheMode(CacheMode.NO_CACHE).tag(context).params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                Log.w(TAG, "onSuccess: "+response.body());
                if (className==null){
                    if (callBack != null) {
                        callBack.netBack(reqTag, response.body());
                    }
                }else{
                    Object object = JSON.parseObject(response.body(), className);
                    if (object==null){
                        Toast.makeText(context, "网络连接失败", Toast.LENGTH_LONG).show();
                    }else if (callBack != null) {
                        callBack.netBack(reqTag, object);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    public static void postJsonData(final Context context, final String path, final int reqTag, Map<String, String> params, final Class className, String loadingStr, final NetCallBack callBack) {

        JSONObject object = new JSONObject(params);
        OkGo.<String>post(path).cacheMode(CacheMode.NO_CACHE).tag(context).upJson(object).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                try{

                    Log.w(TAG, "onSuccess: "+response.body());
                    if (className==null){
                        if (callBack != null) {
                            callBack.netBack(reqTag, response.body());
                        }
                    }else{
                        Object object = JSON.parseObject(response.body(), className);
                        if (object==null){
                            Toast.makeText(context, "网络连接失败", Toast.LENGTH_LONG).show();
                        }else if (callBack != null) {
                            callBack.netBack(reqTag, object);
                        }
                    }
                }catch (Exception o){
                    o.printStackTrace();
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    public static void postStringData(final Context context, final String path, final int reqTag, String params, final Class className, String loadingStr, final NetCallBack callBack) {

        OkGo.<String>post(path).cacheMode(CacheMode.NO_CACHE).tag(context).upString(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                Log.w(TAG, "onSuccess: "+response.body());
                if (className==null){
                    if (callBack != null) {
                        callBack.netBack(reqTag, response.body());
                    }
                }else{
                    Object object = JSON.parseObject(response.body(), className);
                    if (object==null){
                        Toast.makeText(context, "网络连接失败", Toast.LENGTH_LONG).show();
                    }else if (callBack != null) {
                        callBack.netBack(reqTag, object);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    public interface NetCallBack {
        void netBack(int requestTag, Object object);
    }

}
